package com.example.lab2.controller;

import com.example.lab2.model.User;
import com.example.lab2.service.BookService;
import com.example.lab2.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("user")
public class UserController extends BaseController{
    private UserService userService;
    private BookService bookService;

    public UserController(UserService userService, BookService bookService) {
        this.userService = userService;
        this.bookService = bookService;
    }

    @RequestMapping("signUp")
    private String signUp(User user){
        if((userService.query("queryByUsername",user))==null){
            userService.create(user);
            return "redirect:/index.jsp";

        }
        session.setAttribute("error","Username is exists...");
        return "redirect:/signUp.jsp";

    }
    @RequestMapping("signIn")
    private String signIn(User user){
        if((user=userService.queryOne("queryByUsernameAndPassword",user))!=null){
            session.setAttribute("user",user);
            session.setAttribute("books",bookService.queryAll());
            return "redirect:/user/user.jsp";
        }
        session.setAttribute("error","username or password is error");
        return "redirect:/index.jsp";
    }
}
